{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "digits=datasets.load_digits()\n",
    "X=digits.data\n",
    "\"\"\"需要用.copy(),否则会y[digits.target==9]变为空,digits为9的标签都为1。需要拷贝\"\"\"\n",
    "y=digits.target.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"生成2分类问题，等于9或不等于9\"\"\"\n",
    "y[digits.target==9]=1\n",
    "y[digits.target!=9]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9805555555555555"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.2)\n",
    "\n",
    "log_reg=LogisticRegression(max_iter=1000000000)#max_iter为迭代次数\n",
    "log_reg.fit(x_train,y_train)\n",
    "log_reg.score(x_test,y_test)#没啥太大意义，想评价一个小概率事情（10%几率），即使假设都没发生这个事情，成功率也是90%多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_log_predict=log_reg.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "318"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"TN\"\"\"\n",
    "def TN(y_true,y_predict):#声明TN值运算\n",
    "    assert len(y_true)==len(y_predict)\n",
    "    return np.sum((y_true==0)&(y_predict==0))\n",
    "\n",
    "TN(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"FP\"\"\"\n",
    "def FP(y_true,y_predict):\n",
    "    assert len(y_true)==len(y_predict)\n",
    "    return np.sum((y_true==0)&(y_predict==1))\n",
    "FP(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"FN\"\"\"\n",
    "def FN(y_true,y_predict):\n",
    "    assert len(y_true)==len(y_predict)\n",
    "    return np.sum((y_true==1)&(y_predict==0))\n",
    "FN(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"TP\"\"\"\n",
    "def TP(y_true,y_predict):\n",
    "    assert len(y_true)==len(y_predict)\n",
    "    return np.sum((y_true==1)&(y_predict==1))\n",
    "TP(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[318,   3],\n",
       "       [  4,  35]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"混淆矩阵\"\"\"\n",
    "def confusion_matrix(y_true,y_predict):\n",
    "    return np.array([\n",
    "        [TN(y_true,y_predict),FP(y_true,y_predict)],\n",
    "        [FN(y_true,y_predict),TP(y_true,y_predict)]\n",
    "    ])\n",
    "confusion_matrix(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"精准率：precision\"\"\"\n",
    "def precision_score(y_true,y_predict):\n",
    "    fp=FP(y_true,y_predict)\n",
    "    tp=TP(y_true,y_predict)\n",
    "    try:\n",
    "        return tp/(tp+fp)\n",
    "    except:\n",
    "        return 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"召回率\"\"\"\n",
    "def recall_score(y_true,y_predict):\n",
    "    tp=TP(y_true,y_predict)\n",
    "    fn=FN(y_true,y_predict)\n",
    "    try:\n",
    "        return tp/(tp+fn)\n",
    "    except:\n",
    "        return 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9210526315789473"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "precision_score(y_test,y_log_predict)#测试准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8974358974358975"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recall_score(y_test,y_log_predict)#测试召回率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SCIKIT-learn中的混淆矩阵、召回率、精准率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[318,   3],\n",
       "       [  4,  35]], dtype=int64)"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"混淆矩阵\"\"\"\n",
    "from sklearn.metrics import confusion_matrix \n",
    "confusion_matrix(y_true=y_test,y_pred=y_log_predict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9210526315789473"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"精准率\"\"\"\n",
    "from sklearn.metrics import precision_score\n",
    "precision_score(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8974358974358975"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"召回率\"\"\"\n",
    "recall_score(y_test,y_log_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
